
package com.aidong.dao.base;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.transform.ResultTransformer;

public class MapTransformer implements ResultTransformer {
    private Map<String, String> fieldMap = new HashMap();

    public MapTransformer() {
    }

    public Object transformTuple(Object[] tuple, String[] aliases) {
        LinkedHashMap map = new LinkedHashMap();

        for(int i = 0; i < aliases.length; ++i) {
            String field = (String)this.fieldMap.get(aliases[i]);
            if(field == null) {
                field = this.getFieldName(aliases[i]);
            }

            map.put(field, tuple[i]);
        }

        return map;
    }

    public List transformList(List list) {
        return list;
    }

    private String getFieldName(String alias) {
        String name = alias.toLowerCase();
        String[] ss = name.split("_");
        if(ss[0].length() > 0) {
            name = ss[0].substring(0, 1).toLowerCase() + ss[0].substring(1);
        } else {
            name = "_";
        }

        for(int i = 1; i < ss.length; ++i) {
            if(ss[i].length() > 0) {
                name = name + ss[i].substring(0, 1).toUpperCase() + ss[i].substring(1);
            }
        }

        if("class".equalsIgnoreCase(name)) {
            name = "jnClass";
        }

        this.fieldMap.put(alias, name);
        return name;
    }
}
